package com.ccy.thread;

import java.util.concurrent.atomic.AtomicInteger;

public class ThreadLocalDemo {
	private final int threadLocalHashCode = nextHashCode();

	/**
	 * The next hash code to be given out. Updated atomically. Starts at zero.
	 */
	private static AtomicInteger nextHashCode = new AtomicInteger();

	/**
	 * The difference between successively generated hash codes - turns implicit
	 * sequential thread-local IDs into near-optimally spread multiplicative
	 * hash values for power-of-two-sized tables.
	 */
	private static final int HASH_INCREMENT = 0x61c88647;

	/**
	 * Returns the next hash code.
	 */
	private static int nextHashCode() {
		return nextHashCode.getAndAdd(HASH_INCREMENT);
	}

	public static void main(String[] args) {
		ThreadLocalDemo test = null;
		for (int i = 0; i < 10; i++) {
			test = new ThreadLocalDemo();
			System.out.println(test.threadLocalHashCode);
		}

	}
}
